package com.zzl.mine.util;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.UUID;

import net.sf.json.JSONObject;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.zzl.mine.tool.ExceptionUtil;
import com.zzl.mine.tool.ResourceUtil;

/**
 * @description 微信开发获取信息---微信开放平台 
 * @author sunny
 * @date 2018年9月27日 上午9:27:38
 */
public class WeChatUitl {
	
	private static final Logger logger = LoggerFactory.getLogger(WeChatUitl.class);

	public static String appId;
	public static String appSecret;
	
	public static String getAppId() {
		return appId;
	}

	public static void setAppId(String appId) {
		WeChatUitl.appId = appId;
	}

	public static String getAppSecret() {
		return appSecret;
	}

	public static void setAppSecret(String appSecret) {
		WeChatUitl.appSecret = appSecret;
	}

	static{
		ResourceUtil ru = new ResourceUtil("resource/wxshare");
		setAppId(ru.getValue("APP_ID"));
		setAppSecret(ru.getValue("APP_SECRET"));
	}

	/**
	 * @description 获取access_token 
	 * @return
	 */
	public static String getAccessToken() {
		String access_token = "";
		String grant_type = "client_credential";//获取access_token填写client_credential
		String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=" + grant_type + "&appid=" + appId
				+ "&secret=" + appSecret;
		try {
			URL urlGet = new URL(url);
			HttpURLConnection http = (HttpURLConnection) urlGet.openConnection();
			http.setRequestMethod("GET"); // 必须是get方式请求 
			http.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
			http.setDoOutput(true);
			http.setDoInput(true);
			System.setProperty("sun.net.client.defaultConnectTimeout", "30000");// 连接超时30秒  
			System.setProperty("sun.net.client.defaultReadTimeout", "30000"); // 读取超时30秒  
			http.connect();
			InputStream is = http.getInputStream();
			int size = is.available();
			byte[] jsonBytes = new byte[size];
			is.read(jsonBytes);
			String message = new String(jsonBytes, "UTF-8");
			logger.info("getAccessToken-->result:"+message);
			JSONObject demoJson = JSONObject.fromObject(message);
			if(demoJson.containsKey("access_token")){
				access_token = demoJson.getString("access_token");
			}
			is.close();
		} catch (Exception e) {
			logger.error("【获取access_token，异常信息】：{}", ExceptionUtil.logExceptionStack(e));
		}
		return access_token;
	}

	/**
	 * @description 获取jsapi_ticket 
	 * @param access_token
	 * @return
	 */
	public static String getTicket(String access_token) {
		String ticket = null;
		String url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=" + access_token + "&type=jsapi";//这个url链接和参数不能变  
		try {
			URL urlGet = new URL(url);
			HttpURLConnection http = (HttpURLConnection) urlGet.openConnection();
			http.setRequestMethod("GET"); // 必须是get方式请求  
			http.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
			http.setDoOutput(true);
			http.setDoInput(true);
			System.setProperty("sun.net.client.defaultConnectTimeout", "30000");// 连接超时30秒  
			System.setProperty("sun.net.client.defaultReadTimeout", "30000"); // 读取超时30秒  
			http.connect();
			InputStream is = http.getInputStream();
			int size = is.available();
			byte[] jsonBytes = new byte[size];
			is.read(jsonBytes);
			String message = new String(jsonBytes, "UTF-8");
			logger.info("getTicket-->result:"+message);
			JSONObject demoJson = JSONObject.fromObject(message);
			if(demoJson.containsKey("ticket")){
				ticket = demoJson.getString("ticket");
			}
			is.close();
		} catch (Exception e) {
			logger.error("【获取jsapi_ticket，异常信息】：{}", ExceptionUtil.logExceptionStack(e));
		}
		return ticket;
	}

	public static String getUUID() {
		return UUID.randomUUID().toString().replace("-", "").substring(0, 16);
	}

	public static String getTimeStamp() {
		return String.valueOf(System.currentTimeMillis() / 1000);
	}

}
